exports = module.exports = function(sio, config, redis) {
    var util = require('util'),
        redis_client1 = redis.createClient(config.redis.port,config.redis.host),
        redis_client2 = redis.createClient(config.redis_pubsub.port,config.redis_pubsub.host)
    
    redis_client1.select(1, redis.print);

    onRedisConnect(redis_client1, 'base');
    onRedisConnect(redis_client2, 'pubsub');

    function onRedisConnect(redis, redisServer) {
        redis.on("error", function(err) {
            console.log("Redis-" + redisServer + " says: " + err);
        });

        redis.on("ready", function() {
            console.log("Redis-" + redisServer + " ready.");
        });

        redis.on("reconnecting", function(arg) {
            console.log("Redis-" + redisServer + " reconnecting: " + JSON.stringify(arg));
        });
    }
    
    //socket.io set
    sio.set('log level', config.socket.log_level)
    sio.set('heartbeat timeout', config.socket.heartbeat_timeout)
    sio.set('heartbeat interval', config.socket.heartbeat_interval)
    
    web_conns = {};
    
    //connection
    sio.sockets.on('connection', function(conn){
        var hs = conn.handshake
        
        util.log('connection - ' + hs.address.address + ':' + hs.address.port + ' - ' + conn.id);

        /*
        conn.on('join', function(data) {
            onJoin(conn, data);
        })
        
        */
        conn.on('disconnect', function(data) {
            onDisconnect(conn, data);
        })
        
        conn.on('login', function(data){
            console.log('login')
            onLogin(conn,data)
        })
    })
    
    function onLogin(conn, data){
        web_conns[conn.id] = conn
        
        redis_client1.hget('HupuRun_postDraft_'+data.auth_key, 'value2', function(err, reply){
            if(reply == data.value2){
                redis_client1.hset('HupuRun_postDraft_'+data.auth_key,'conn_id',conn.id)
            }
        })
    }
    
    function onDisconnect(conn, data) {
        delete web_conns[conn.id]
        
        var hs = conn.handshake;
        util.log('disconnect - ' + hs.address.address + ':' + hs.address.port + ' - ' + conn.id);
    }
    
    redis_client2.subscribe('HupuRun_postDraft_notify')
    
    redis_client2.on('message', function(channel, message){
        util.log("client1 channel " + channel + ": " + message);
        if(channel != 'HupuRun_postDraft_notify'){
            return
        }
        message = JSON.parse(message)
        redis_client1.hgetall('HupuRun_postDraft_' + message.auth_key, function(err,obj){
            if(!obj){
                return
            }
            var uid = obj.uid,
                news_draft_id = obj.news_draft_id,
                conn = web_conns[obj.conn_id]
            if(conn){
                conn.emit('success',{"token":message.token,"news_draft_id":news_draft_id})
            }
        })
    })
};